package com.artisan.admin.controllers;

import com.artisan.admin.model.adViewModels.AdViewModel;
import com.artisan.admin.model.startPageViewModels.StartPageViewModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.adResultModels.AdResultModel;
import com.artisan.admin.resultModels.startPageResultModels.StartPageResultModel;
import com.artisan.domain.dto.AdDto;
import com.artisan.domain.dto.PageDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.serviceInterface.IStartPageService;
import com.sun.xml.bind.v2.model.core.ID;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;

/** 启动页控制器 */
@RequestMapping("/api/startPage")
@RestController
@Api("启动页模块")
@ApiOperation(value = "启动页模块")
public class StartPageController {
  private final IStartPageService startPageService;

  public StartPageController(IStartPageService startPageService) {
    this.startPageService = startPageService;
  }

  /**
   * 获得启动页列表
   *
   * @param pageIndex 当前页
   * @param pageSize 每页数
   */
  // GET /list
  @ApiOperation(value = "获得启动页列表")
  @ApiImplicitParams({
    @ApiImplicitParam(
        paramType = "query",
        name = "pageIndex",
        dataType = "Int",
        required = true,
        value = "当前页，从0开始",
        defaultValue = "0"),
    @ApiImplicitParam(
        paramType = "query",
        name = "pageSize",
        dataType = "Int",
        required = true,
        value = "每页数",
        defaultValue = "10")
  })
  @ApiResponses(@ApiResponse(code = 200, response = StartPageResultModel.class, message = "成功"))
  @RequestMapping(
      value = {"/list"},
      method = {RequestMethod.GET})
  public MessageBase getList(
      @RequestParam(value = "pageIndex") int pageIndex,
      @RequestParam(value = "pageSize") int pageSize) {
    MessageBase result = new MessageBase();
    PageDto<StartPageDto> page = this.startPageService.findList(pageIndex, pageSize);
    result.setPageIndex(page.getNumber());
    result.setPageSize(page.getSize());
    result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

    ArrayList<StartPageResultModel> models = new ArrayList<>();

    page.getContent()
        .forEach(
            dto -> {
              StartPageResultModel model = new StartPageResultModel();
              model.setId(dto.getId());
              model.setNumber(dto.getNumber());
              model.setName(dto.getName());
              model.setStartDate(dto.getStartDate());
              model.setEndDate(dto.getEndDate());
              model.setPosition(dto.getPosition());
              model.setStayTime(dto.getStayTime());
              model.setAddDate(dto.getAddDate());
              model.setStatus(dto.getStatus());

              models.add(model);
            });

    result.setData(models);
    return result;
  }

    /**
     * 添加启动页
     */
    // POST /add
    @ApiOperation(value = "添加启动页")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value ={"/add"}, method = {RequestMethod.POST})
    public MessageBase postAdd(@RequestBody StartPageViewModel model) {
        MessageBase result = new MessageBase();

        StartPageDto dto = new StartPageDto();
        dto.setName(model.getName());
        dto.setStartDate(model.getStartDate());
        dto.setEndDate(model.getEndDate());
        dto.setPosition(model.getPosition());
        dto.setStayTime(model.getStayTime());
        dto.setContent(model.getContent());

        int id = startPageService.add(dto);

        result.setSuccess(id > 0);

        return result;
    }

    /**
     * 删除启动页
     * @return 状态
     */
    // DELETE /{1}
    @ApiOperation(value = "删除启动页")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "id",
                    dataType = "Int",
                    required = true,
                    value = "id",
                    defaultValue = "1")
    })
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value ={"/{id}"}, method = {RequestMethod.DELETE})
    public MessageBase delete(@PathVariable int id) {
        MessageBase result = new MessageBase();
        boolean b = this.startPageService.delete(id);

        result.setSuccess(b);
        return result;
    }

    /**
     * 修改
     * @return 状态
     */
    // PUT /
    @ApiOperation(value = "更新启动页")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(method = {RequestMethod.PUT})
    public MessageBase put(@RequestBody StartPageViewModel model) {
        MessageBase result = new MessageBase();

        StartPageDto dto = new StartPageDto();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setStartDate(model.getStartDate());
        dto.setEndDate(model.getEndDate());
        dto.setPosition(model.getPosition());
        dto.setStayTime(model.getStayTime());
        dto.setContent(model.getContent());

        boolean b = this.startPageService.update(dto);

        result.setSuccess(b);

        return result;
    }

    /**
     * 修改状态
     */
    // PATCH /status
    @ApiOperation(value = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "status",
                    dataType = "Int",
                    required = true,
                    value = "状态",
                    defaultValue = "1")
    })
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value ={"/status/{id}"}, method = {RequestMethod.DELETE})
    public MessageBase patchStatus(@PathVariable int id, @RequestParam(value = "status") int status) {
        MessageBase result = new MessageBase();
        boolean b = this.startPageService.updateStatus(id, status);

        result.setSuccess(b);
        return result;
    }
}
